#!/usr/bin/python
# -*- encoding: utf-8 -*-
'''
@File    :   6.py
@Time    :   2020/05/26 20:45:38
@Author  :   Malygos_l 
@Version :   1.0
@Contact :   751756061@qq.com
@WebSite :   www.gitee.com/malygos_l
'''
# Start typing your code from here
'''
用面向对象,实现一个学生Python成绩管理系统;
学生的信息存储在文件中;学生信息的字段有(班级,学号,姓名, Python成绩)
实现对学生信息及成绩的增,删,改,查方法;
'''

import time


class Student:
    students = []  #存储结构

    #初始化
    def __init__(self, Student_info):
        self.number = Student_info[0]
        self.name = Student_info[1]
        self.se_class = Student_info[2]
        self.python_score = Student_info[3]
        Student.students.append(self)

    #信息存入文件
    @classmethod
    def write_records(cls):
        try:
            with open("homework6/Student_info.txt", "w",
                      encoding="UTF-8") as f:
                Student_info = []
                for student in cls.students:
                    Student_info.append("{0} {1} {2} {3}\n".format(
                        student.number, student.name, student.se_class,
                        student.python_score))
                f.writelines(Student_info)

        except IOError as e:
            raise e

    #打印学生信息
    def Print__student_info(self):
        print("学号：{0}\t姓名：{1}\t班级：{2}\tPython成绩：\t{3}".format(
            self.number, self.name, self.se_class, self.python_score))

    #增加学生记录
    @classmethod
    def add_record(cls):
        info = []
        flag = 1
        input_number = ''
        while (flag):
            flag = 0
            input_number = input("学号：")
            for student in cls.students:
                if input_number == student.number:
                    print("学号重复，请重新输入！")
                    flag = 1
        info.append(input_number)
        info.append(input("姓名："))
        info.append(input("班级："))
        info.append(input("Python成绩："))
        Student(info)
        print("添加成功！")

    #删除学生记录
    @classmethod
    def del_record(cls):
        number = input("请输入你想删除的学生记录的学号：")
        no = 0
        for student in cls.students:
            if number == student.number:
                del cls.students[no]
                print("删除成功！")
                return
            no += 1
        print("无该学生记录！")

    #修改学生记录
    @classmethod
    def change_record(cls):
        number = input("请输入你想修改的学生记录的学号：")
        no = 0  #计数器/序号
        for student in cls.students:
            if number == student.number:
                i = input('''
                    请输入你想修改的内容：
                        1 学号
                        2 姓名
                        3 班级
                        4 成绩
                    （输入序号）
                    ''')
                if i == '1':
                    flag = 1
                    new_number = ''
                    while(flag):
                        flag = 0
                        new_number = input("请输入新的学号：")
                        for student in cls.students:
                            if new_number == student.number:
                                print("学号重复，请重新输入！")
                                flag = 1    
                    cls.students[no].number = new_number
                    print("修改成功！")
                    return
                elif i == '2':
                    new_name = input("请输入新的姓名：")
                    cls.students[no].name = new_name
                    print("修改成功！")
                    return
                elif i == '3':
                    new_class = input("请输入新的班级：")
                    cls.students[no].se_class = new_class
                    print("修改成功！")
                    return
                elif i == '4':
                    new_score = input("请输入新的成绩：")
                    cls.students[no].python_score = new_score
                    print("修改成功！")
                    return
                else:
                    print("输入序号有误！")
                    return
            no += 1
        print("无该学生记录！")

    #查询学生记录
    @classmethod
    def inquire_record(cls):
        i = input('''
            请输入你要查找的方式：
                1 以学号查询
                2 以姓名查询
                3 以班级查询
                4 以Python成绩范围查询（输入一个成绩，
                  输出大于等于该成绩的学生的学生信息）
            ''')
        flag = 0
        if i == '1':
            inquire_number = input("请输入学号：")
            for student in cls.students:
                if inquire_number == student.number:
                    student.Print__student_info()
                    return
            # print("无搜索结果！")
        elif i == '2':
            inquire_name = input("请输入姓名：")
            for student in cls.students:
                if inquire_name == student.name:
                    student.Print__student_info()
                    flag = 1
            # print("无搜索结果！")
        elif i == '3':
            inquire_class = input("请输入班级：")
            for student in cls.students:
                if inquire_class == student.se_class:
                    student.Print__student_info()
                    flag = 1
            # print("无搜索结果！")
        elif i == '4':
            inquire_score = input("请输入成绩：")
            for student in cls.students:
                if int(inquire_score) <= int(student.python_score):
                    student.Print__student_info()
                    flag = 1
            # print("无搜索结果！")
        else:
            print("输入序号有误！")
            return
        if flag == 0:
            print("无搜索结果！")


#功能表
def func_table(Student):
    flag = 1
    while (flag):
        print("""
    -----------功能-----------    
        "1": "打印原始数据"
        "2": "增加学生记录"
        "3": "删除学生记录"
        "4": "修改学生记录"
        "5": "查询学生记录"
        "0": "退出"

        说明：每次执行完方法后，均需等待两秒返回主界面
        """)
        i = input("请选择你要进行的方法（输入序号）:")
        if i == '1':
            for student in Student.students:
                student.Print__student_info()
        elif i == '2':
            Student.add_record()
        elif i == '3':
            Student.del_record()
        elif i == '4':
            Student.change_record()
        elif i == '5':
            Student.inquire_record()
        elif i == '0':
            flag = 0
            print("退出！")
            return
        else:
            print("输入序号有误，请重新输入！")
        time.sleep(2)


#读取文件信息
def read_info():
    try:
        with open("homework6/Student_info.txt", "r", encoding="UTF-8") as f:
            info = f.readlines()
            Student_info = []
            for line in info:
                Student_info.append(line.split())
            for student in Student_info:
                Student(student)
    except IOError as e:
        raise e


def main():
    read_info()  #读取文件信息并创建Student对象
    func_table(Student)  #系统功能
    Student.write_records()  #将更新后的信息写入文件


if __name__ == "__main__":
    main()